import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  // 状态
  const sidebarCollapsed = ref(false)
  const theme = ref(localStorage.getItem('theme') || 'light')
  const language = ref(localStorage.getItem('language') || 'zh-CN')
  const loading = ref(false)
  const breadcrumbs = ref([])
  
  // 系统配置
  const systemConfig = ref({
    title: '医院管理系统',
    version: '1.0.0',
    copyright: '© 2024 医院管理系统 All Rights Reserved'
  })
  
  // 通知相关
  const notifications = ref([])
  const unreadCount = ref(0)
  
  // 操作
  const toggleSidebar = () => {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }
  
  const setSidebarCollapsed = (collapsed) => {
    sidebarCollapsed.value = collapsed
  }
  
  const setTheme = (newTheme) => {
    theme.value = newTheme
    localStorage.setItem('theme', newTheme)
    // 应用主题到DOM
    document.documentElement.setAttribute('data-theme', newTheme)
  }
  
  const setLanguage = (lang) => {
    language.value = lang
    localStorage.setItem('language', lang)
  }
  
  const setLoading = (isLoading) => {
    loading.value = isLoading
  }
  
  const setBreadcrumbs = (crumbs) => {
    breadcrumbs.value = crumbs
  }
  
  const addBreadcrumb = (crumb) => {
    breadcrumbs.value.push(crumb)
  }
  
  const clearBreadcrumbs = () => {
    breadcrumbs.value = []
  }
  
  // 通知管理
  const addNotification = (notification) => {
    const newNotification = {
      id: Date.now(),
      timestamp: new Date(),
      read: false,
      ...notification
    }
    notifications.value.unshift(newNotification)
    updateUnreadCount()
  }
  
  const markAsRead = (id) => {
    const notification = notifications.value.find(n => n.id === id)
    if (notification) {
      notification.read = true
      updateUnreadCount()
    }
  }
  
  const markAllAsRead = () => {
    notifications.value.forEach(n => n.read = true)
    updateUnreadCount()
  }
  
  const removeNotification = (id) => {
    const index = notifications.value.findIndex(n => n.id === id)
    if (index > -1) {
      notifications.value.splice(index, 1)
      updateUnreadCount()
    }
  }
  
  const clearNotifications = () => {
    notifications.value = []
    unreadCount.value = 0
  }
  
  const updateUnreadCount = () => {
    unreadCount.value = notifications.value.filter(n => !n.read).length
  }
  
  // 初始化应用
  const initApp = () => {
    // 应用保存的主题
    document.documentElement.setAttribute('data-theme', theme.value)
    
    // 模拟一些初始通知
    const initialNotifications = [
      {
        type: 'warning',
        title: '库存预警',
        message: '阿莫西林胶囊库存不足，请及时补充',
        category: 'system'
      },
      {
        type: 'info',
        title: '系统维护',
        message: '系统将于今晚2:00-4:00进行维护',
        category: 'maintenance'
      }
    ]
    
    initialNotifications.forEach(notification => {
      addNotification(notification)
    })
  }
  
  return {
    // 状态
    sidebarCollapsed,
    theme,
    language,
    loading,
    breadcrumbs,
    systemConfig,
    notifications,
    unreadCount,
    
    // 操作
    toggleSidebar,
    setSidebarCollapsed,
    setTheme,
    setLanguage,
    setLoading,
    setBreadcrumbs,
    addBreadcrumb,
    clearBreadcrumbs,
    addNotification,
    markAsRead,
    markAllAsRead,
    removeNotification,
    clearNotifications,
    initApp
  }
}) 